//
//  MapViewController.m
//  penzer
//
//  Created by 还带大道 on 2018/11/13.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import "SPAnnotation.h"
#import "SPAnnotationView.h"

@interface MapViewController ()<MKMapViewDelegate>
@property(nonatomic,strong) CLLocationManager *locMgr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topMargin;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation:YES andTitle:@"门店详情" andNaviColor:[UIColor whiteColor] andTitleColor:nil];
    self.topMargin.constant = kTopHeight;
    
    // authorization:授权
    [self.locMgr requestAlwaysAuthorization];
    self.mapView.mapType = MKMapTypeStandard;
    self.mapView.delegate = self;
    
    //1 跟踪用户当前位置
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
  
    
    //3 添加大头针到地图上
    SPAnnotation *annotaion = [[SPAnnotation alloc] init];
    annotaion.coordinate = CLLocationCoordinate2DMake(30.570336929449965, 104.06092342565677);
    annotaion.title = @"中国移动(环球中心)";
    annotaion.icon = @"icon_rollview_gift";
    [self.mapView addAnnotation:annotaion];
}





#pragma mark -MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    //判断是否为自定义的大头针模型 如果不是则返回nil 代表采取系统默认的大头针样式
    if(![annotation isKindOfClass:[SPAnnotation class]]) return nil;
    SPAnnotationView *annoView = [SPAnnotationView annotationViewWithMapView:mapView];
    annoView.onClickAnnotationViewBlock = ^(SPAnnotationView *view) {//点击了大头针
        RZLog(@"---%@",view.annotation.title);
    };
    
    annoView.annotation = annotation;
    return annoView;
}






- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    //取出用户当前的经纬度
    CLLocationCoordinate2D center = userLocation.location.coordinate;
    //设置地图的显示范围span
    MKCoordinateSpan span = MKCoordinateSpanMake(0.01,0.01);
    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
    [mapView setRegion:region animated:YES];
}



#pragma mark--setter&&getter

-  (CLLocationManager *)locMgr
{
    if(!_locMgr){
        _locMgr = [[CLLocationManager alloc] init];
    }
    return _locMgr;
}
@end
